#!/usr/bin/env python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""DefaultHandler renders the default template of gcategorically."""

__author__ = 'Vic Fryzel <vicfryzel@google.com>'

import logging
import os
import re
import atom
import gdata.client
import gdata.data
import gdata.docs.data
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from gcategorically.oauth_handler import OAuthTokenHandler


class DefaultHandler(blobstore_handlers.BlobstoreUploadHandler):
  def get(self):
    th = OAuthTokenHandler()
    client = th.GetAuthorizedClient()
    if client is None:
      self.redirect('/auth')
      return

    template_values = {
        'upload_url': blobstore.create_upload_url('/collections.json')
    }
    path = os.path.join(os.path.dirname(__file__), 'templates/index.html')
    self.response.out.write(template.render(path, template_values))


# The application container that runs a DefaultHandler and maps it to th / URI.
application = webapp.WSGIApplication(
    [('/', DefaultHandler)],
    debug=True)


def main():
  """Run the DefaultHandler application."""
  run_wsgi_app(application)


if __name__ == "__main__":
  main()
